<?php

namespace app\index\model;

use think\Request;
use think\Model;

class MemberAddress extends Model
{

    // 模型事件
    protected static function init()
    {
        // 写入前
        self::event('before_write', function ($data) {
            $data->member_id = Request::instance()->param('member_id');
            $data->name = Request::instance()->param('name');
            $data->phone = Request::instance()->param('phone');
            $data->area_id = Request::instance()->param('area_id');
            $data->area_info = Request::instance()->param('area_info');
            $data->address = Request::instance()->param('address');
            $data->status = Request::instance()->param('status');
            $data->create_time = date('Y-m-d H:i:s');
        });

        // 写入后
        self::event('after_write', function ($data) {
            if ($data->status == 1) {
                self::where(['member_id' => $data->member_id, 'id' => ['neq', $data->id]])->update(['status' => '2']);
            }


        });

        // 删除后
        self::event('after_delete',function ($data) {
           $count = self::where(['member_id'=>$data->member_id])->count();
           if ($count == 1) {
               self::where(['member_id'=>$data->member_id])->update(['status' => '1']);
           }
        });
    }

}
